package com.jackrain.nea.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ReferenceUtil;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.face.impl.UserImpl;
import com.jackrain.nea.web.query.DefaultWebEvent;
import com.jackrain.nea.web.query.QuerySession;
import com.jackrain.nea.web.query.QuerySessionImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * Created by Lucky dog on 2017/7/31.
 */
@RestController
@Slf4j
public class TestController {

//    @Autowired
//    EmailService  emailService;


    @RequestMapping(path = "/p/c/testCommand",method = RequestMethod.GET)
    public JSONObject testCommand(HttpServletRequest request,
                                  @RequestParam(value="name", required=true)String name) throws Exception {
        QuerySession session = new QuerySessionImpl();
        DefaultWebEvent defaultWebEvent = new DefaultWebEvent("test",request,false);
        session.setEvent(defaultWebEvent);
        Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.sys.Command", "CommandTest", "3.0");
        ValueHolder vh= (ValueHolder) o.getClass().getDeclaredMethod("execute", new Class[]{QuerySession.class}).invoke(o, session);
        log.info("------------------"+JSON.toJSONString(vh)+"--------------------");
        return (JSONObject) JSON.toJSON(vh);
    }


    @RequestMapping(path = "/p/c/testMap",method = RequestMethod.GET)
    public JSONObject testMap(HttpServletRequest request,
                        @RequestParam(value="name", required=true)String name) throws Exception {
        HashMap map = new HashMap();
        map.put("name","yaojing");
        Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.sys.Command", "MapTest", "3.0");
        ValueHolder vh= (ValueHolder) o.getClass().getDeclaredMethod("execute", new Class[]{HashMap.class}).invoke(o,map);
        log.info("------------------"+JSON.toJSONString(vh)+"--------------------");
        return (JSONObject) JSON.toJSON(vh);
    }

    @RequestMapping(path = "/p/cs/testSession",method = RequestMethod.GET)
    public JSONObject testSession(HttpServletRequest request) throws Exception {
        JSONObject jo = new JSONObject();
        UserImpl userWeb= (UserImpl)request.getSession().getAttribute("user");
        jo.put("code",0);
        jo.put("data",userWeb);
        return  jo;
    }
}
